<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="姓名" prop="name">
                <a-input v-model="queryParam.name"  allow-clear />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="手机号" prop="mobile">
                <a-input v-model="queryParam.mobile" allow-clear />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="地址" prop="addressCode">
                <a-select v-model="queryParam.addressCode" placeholder="请输入地址" allow-clear >
                  <a-select-option v-for="(d, index) in dictOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="!advanced && 6 || 24" :sm="24">
              <span class="table-page-search-submitButtons"
                :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <a-button type="primary" @click="$refs.createForm.handleAdd()" v-hasPermi="['system:milkMember:add']">
          <a-icon type="plus" />新增
        </a-button>
        <a-button type="primary" :disabled="single" @click="$refs.createForm.handleUpdate(undefined, ids)"
          v-hasPermi="['system:milkMember:edit']">
          <a-icon type="edit" />修改
        </a-button>
        <a-button type="danger" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:milkMember:remove']">
          <a-icon type="delete" />删除
        </a-button>
        <table-setting :style="{float: 'right'}" :table-size.sync="tableSize" v-model="columns"
          :refresh-loading="loading" @refresh="getList" />
      </div>
      <!-- 增加修改 -->
      <create-form ref="createForm" @ok="getList" />
      <!-- 数据展示 -->
      <a-table  :loading="loading" :size="tableSize" rowKey="id" :columns="columns" :data-source="list"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :pagination="false"
        :bordered="tableBordered">
        <span slot="operation" slot-scope="text, record">
          <a-divider type="vertical" v-hasPermi="['system:milkMember:edit']" />
          <a @click="handleSnOpen(record)" v-hasPermi="['system:milkMember:edit']">
            送奶计划
          </a>
          <a-divider type="vertical" v-hasPermi="['system:milkMember:remove']" />
          <a @click="toCardPage(record)" v-hasPermi="['system:milkMember:remove']">
            会员卡
          </a>
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination class="ant-table-pagination" show-size-changer show-quick-jumper :current="queryParam.pageNum"
        :total="total" :page-size="queryParam.pageSize" :showTotal="total => `共 ${total} 条`"
        @showSizeChange="onShowSizeChange" @change="changeSize" />
    </a-card>
    <a-modal v-model:visible="snOpen" title="送奶计划" @ok="handleOk">
      <a-form-model ref="snForm" :model="snForm" >
        <a-form-item label="奶品">
          <a-select v-model="snForm.type" placeholder="请选择奶品" allow-clear >
            <a-select-option v-for="(d, index) in milkTypeOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="配送">
          <a-select v-model="snForm.delivery" placeholder="请选择配送类型" allow-clear >
            <a-select-option v-for="(d, index) in dTypeOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="数量">
          <a-input v-model="snForm.count" placeholder="请输入数量"></a-input>
        </a-form-item>
        <a-form-item label="送奶日期">
          {{ date }}
        </a-form-item>
        <a-form-item label="时间">
          <a-time-picker format="HH" v-model="snForm.time" valueFormat="HH"/>
        </a-form-item>
        <a-form-item label="备注">
          <a-input v-model="snForm.remark" placeholder="请输入备注"></a-input>
        </a-form-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { listMilkMember, delMilkMember } from '@/api/system/milkMember'
import { getMilkDictOp } from '@/api/system/milkDict'
import moment from 'moment'
import { saveRecord } from '@/api/system/milkRecord'
import CreateForm from './modules/CreateForm'
import { tableMixin } from '@/store/table-mixin'

export default {
  name: 'MilkMember',
  components: {
    CreateForm
  },
  mixins: [tableMixin],
  data () {
    return {
      date:'',
      snForm:{},
      snOpen:false,
      dTypeOptions:[],
      milkTypeOptions:[],
      dictOptions:[],
      list: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 查询参数
      queryParam: {
        name: null,
        mobile: null,
        address: null,
        addressCode: null,
        pageNum: 1,
        pageSize: 10
      },
      columns: [
        {
          title: '姓名',
          dataIndex: 'name',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '手机号',
          dataIndex: 'mobile',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '剩余数量',
          dataIndex: 'remain',
          ellipsis: true,
          align: 'center'
        },{
          title: '地址',
          dataIndex: 'address',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '18%',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  filters: {
  },
  created () {
    this.date = moment().add(1,"days").format("YYYY-MM-DD")
    this.getList()
    this.getDictList('address')
    this.getMilkType()
    this.getDelivery()
  },
  computed: {
  },
  watch: {
  },
  methods: {
    handleOk(){
      this.snForm.timeStr = this.date + " " + this.snForm.time + ":00:00"
      let req = this.snForm
      req.time = this.date
      saveRecord(req).then(resp => {
        if (resp.code == 200) {
          this.$message.success('新增送奶计划成功！',3)
        }else {
          this.$message.error('新增送奶计划失败！',3)
        }
        this.getList()
        this.snOpen = false
      })
    },
    getMilkType(){
      getMilkDictOp({code:'milkType'}).then(resp => {
        this.milkTypeOptions = resp
      })
    },
    getDelivery(){
      getMilkDictOp({code:'Delivery'}).then(resp => {
        this.dTypeOptions = resp
      })
    },
    handleSnOpen(record){
      this.snForm = {}
      this.snForm.count = 1
      this.snForm.time = record.time
      this.snForm.remark = record.remark
      this.snForm.type = record.type
      this.snForm.delivery = record.delivery
      this.snForm.memberId = record.id
      this.snForm.memberName = record.name
      this.snForm.addressCode = record.addressCode
      this.snOpen = true
    },
    toCardPage(record){
      this.$router.push({path:'/milk/memberMilkCard',query: {memberId:record.id}})
    },
    getDictList(code){
      getMilkDictOp({code:code}).then(resp => {
        this.dictOptions = resp
      })
    },
    /** 查询牛奶会员列表 */
    getList () {
      this.loading = true
      listMilkMember(this.queryParam).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        name: undefined,
        mobile: undefined,
        address: undefined,
        addressCode: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const ids = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中编号为' + ids + '的数据',
        onOk () {
          return delMilkMember(ids)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('system/milkMember/export', {
            ...that.queryParam
          }, `milkMember_${new Date().getTime()}.xlsx`)
        },
        onCancel () {}
      })
    }
  }
}
</script>
